package com.headwind.logistics.permission.controller;

import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultCode;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.permission.dto.MenuRoleDTO;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.pojo.TreeTableModel;
import com.headwind.logistics.permission.service.intl.MenuServiceIntl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-14 17:06
 * @Version 1.0
 * @DESCRIPTION 菜单控制器层
 */
@Controller
public class MenuController {
    //菜单业务逻辑层接口
    @Resource
    private MenuServiceIntl menuServiceIntl;
    //响应api接口
    @Resource
    private ResultGenerator resultGenerator;
    /**
     * 树形表格查询
     * @return
     */
    @RequestMapping("/admin/queryMenuRole")
    @ResponseBody
    public Object queryMenuRole(Integer page,Integer limit){
        //获取菜单列表
        List<MenuRoleDTO> menuRoleDTOS = menuServiceIntl.queryMenuRolePage(null, null);

        return new TreeTableModel(menuRoleDTOS);
    }

    /**
     * 树形表格查询  使用resultMap
     * @return
     */
    @RequestMapping("/admin/queryMenuRoleMap")
    @ResponseBody
    public Object queryMenuRoleMap(Integer page,Integer limit){

        List<Menu> menuList = menuServiceIntl.queryMenuRolePageMap(null, null);

        return new TreeTableModel(menuList);
    }

    /**
     * 添加 菜单角色关联表
     * @param menuId
     * @param roleId
     * @return
     */
    @PostMapping("/admin/addMenuRole/{menuId}/{roleId}")
    @ResponseBody
    public RestResult addMenuRole(@PathVariable("menuId")Long menuId,@PathVariable("roleId") Long roleId){

        try{

            int count = menuServiceIntl.addMenuRole(menuId, roleId);

            if (count>0){
                return resultGenerator.getSuccessResult();
            }
            return resultGenerator.getFailResult("操作失败");

        }catch (Exception e){
            return resultGenerator.getFreeResult(ResultCode.INTERNAL_SERVER_ERROR,"服务器错误",null);
        }


    }

    /**
     * 删除角色菜单关联表
     * @param menuId
     * @param roleId
     * @return
     */
    @PostMapping("/admin/deleteMenuRole/{menuId}/{roleId}")
    @ResponseBody
    public RestResult deleteMenuRole(@PathVariable("menuId")Long menuId,@PathVariable("roleId") Long roleId){

        try{

            int count = menuServiceIntl.deleteMenuRole(menuId, roleId);

            if (count>0){
                return resultGenerator.getSuccessResult();
            }
            return resultGenerator.getFailResult("操作失败哦！");

        }catch(Exception e){

            return resultGenerator.getFreeResult(ResultCode.INTERNAL_SERVER_ERROR,"服务器错误",null);
        }
    }

    /**
     * 根据菜单id删除菜单
     * @param menuId
     * @return
     */
    @PostMapping("/admin/deleteMenuByMenuId/{menuId}")
    @ResponseBody
    public RestResult deleteMenuByMenuId(@PathVariable("menuId") Long menuId){

        Integer count = menuServiceIntl.deleteMenuByMenuId(menuId);

        if (count >0){
            return resultGenerator.getSuccessResult("操作成功",null);
        }

        return resultGenerator.getFailResult("操作失败");
    }

    /**
     * 获取全部菜单
     * @return
     */
    @GetMapping("/admin/queryAllMenu")
    @ResponseBody
    public RestResult queryAllMenu(){

        List<Menu> menuList = menuServiceIntl.queryAllMenu();

        return resultGenerator.getSuccessResult(menuList);
    }

    /**
     * 添加菜单
     * @param roleIdList
     * @param menu
     * @return
     */
    @PostMapping("/admin/addMenu")
    @ResponseBody
    public RestResult addMenu(String roleIdList,Menu menu){

        Integer count = menuServiceIntl.addMenu(menu, roleIdList);
        if (count > 0){
            return resultGenerator.getSuccessResult(count);
        }
        return resultGenerator.getFailResult("更改失败");
    }

}
